package com.itboye.shop.me;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.shop.R;
import com.itboye.shop.base.BaseActivity;
import com.itboye.shop.bean.PersonDataBean;
import com.itboye.shop.common.MyEditText;
import com.itboye.shop.logincontroller.LoginController;
import com.itboye.shop.logincontroller.LoginedState;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.UserPresenter;
import com.itboye.shop.util.loadutil.Const;
import com.itboye.shop.util.loadutil.MAlert;
import com.itboye.shop.util.loadutil.SPUtils;

import java.util.Observable;
import java.util.Observer;

/**
 * 通过手机号登陆
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener, Observer {

    TextView txt_forgetpwd, txt_register;
    Button login_phone_ok,login_phone_login;
    MyEditText login_pwd, login_phone;
    TextView tvTitle;
    ImageView back;
    private UserPresenter userPresenter;
    ToggleButton login_tb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_login_to_phone);
        tvTitle.setText("登录");
        login_phone.setText("15736243109");
        login_pwd.setText("123456");
        userPresenter = new UserPresenter(this);
        init();
    }

    private void init() {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.login_phone_ok:
                String phone = login_phone.getText().toString();
                String pwd = login_pwd.getText().toString();
                if (phone.equals("")) {
                    MAlert.alert("请输入账号", Gravity.CENTER);
                    return;
                }
                if (pwd.equals("")) {
                    MAlert.alert("请输入密码", Gravity.CENTER);
                    return;
                }
                showProgressDialog("登陆中,请稍后",true);
                userPresenter.login(phone, pwd);
                break;
            case R.id.login_phone_login:
//                String phone = login_phone.getText().toString();
//                String pwd = login_pwd.getText().toString();
//                if (phone.equals("")) {
//                    MAlert.alert("请输入账号", Gravity.CENTER);
//                    return;
//                }
//                if (pwd.equals("")) {
//                    MAlert.alert("请输入密码", Gravity.CENTER);
//                    return;
//                }
//                userPresenter.login(phone, pwd);
                break;
            case R.id.txt_forgetpwd://忘记密码
                intent = new Intent(this, ForgetPwdActivity.class);
                startActivity(intent);
                break;
            case R.id.txt_register:
                intent = new Intent(this, RegisterActivity.class);
                startActivity(intent);
                break;
            case R.id.back:
                finish();
                break;
            case R.id.login_tb:
                if (login_tb.isChecked()) {
                    login_pwd
                            .setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    login_pwd.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);

                }
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.login_success) {
                PersonDataBean personBean= (PersonDataBean) entity.getData();
                if (personBean!=null) {
                    putSharePre(personBean);
                    MAlert.alert(entity.getData(),Gravity.CENTER);
                    finish();
                }
            } else if (entity.getEventType() == UserPresenter.login_fail) {
                MAlert.alert(entity.getData(),Gravity.CENTER);
            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }

    private void putSharePre(PersonDataBean bean) {
        SPUtils.put(LoginActivity.this, null,
                Const.PaySecret, bean.getPaySecret());
        SPUtils.put(LoginActivity.this, null,
                Const.RELE, bean.getRolesInfo().get(0).getGroupId());
        SPUtils.put(LoginActivity.this, null,
                Const.ID, bean.getId());
        SPUtils.put(LoginActivity.this, null,
                Const.USERNAME, bean.getUsername());
        SPUtils.put(LoginActivity.this, null,
                Const.PASSWORD, bean.getPassword());
        SPUtils.put(LoginActivity.this, null,
                Const.MOBILE, bean.getMobile());
        SPUtils.put(LoginActivity.this, null,
                Const.IS_LOGINED, true);
        SPUtils.put(LoginActivity.this, null,
                Const.HEAD, bean.getHead());
        SPUtils.put(LoginActivity.this, null,
                Const.NICK, bean.getNickname());
        SPUtils.put(LoginActivity.this, null,
                Const.ISAUTH, bean.getRolesInfo().get(0).getIsAuth());
        SPUtils.put(LoginActivity.this, null,
                Const.S_ID, bean.getAutoLoginCode());

        LoginController.setLoginState(new LoginedState());
    }
}
